package com.zf.apartment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zf.apartment.domain.*;
import com.zf.apartment.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zf.apartment.service.IRoomService;

/**
 * 房间管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private FlatsRoomMapper flatsRoomMapper;
    @Autowired
    private FlatsMapper flatsMapper;
    @Autowired
    private RoomImageMapper roomImageMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private RoomPropertyRoomdataMapper roomPropertyRoomdataMapper;
    @Autowired
    private PropertyRoomdataMapper propertyRoomdataMapper;
    String IMAGETYPE = "3";

    @Override
    public Long selectRoomByRoomName(String roomName) {
        return roomMapper.selectRoomByRoomName(roomName);
    }


    /**
     * 查询房间管理
     *
     * @param roomId 房间管理主键
     * @return 房间管理
     */
    @Override
    public Room selectRoomByRoomId(Long roomId) {
        Room room1 = roomMapper.selectRoomByRoomId(roomId);
        room1.getFlatsList().forEach(flats -> {
            room1.setFlatsName(flats.getFlatsName());
            room1.setFlatsId(flats.getFlatsId());
        });
        RoomImage roomImage =roomImageMapper.selectRoomImageByRoomId(room1.getRoomId());
        if(roomImage!=null) {
            if(imageMapper.selectImageByImageId(roomImage.getImageId())!=null) {
                Image image = imageMapper.selectImageByImageId(roomImage.getImageId());
                room1.setRoomImagePath(image.getImagePath());
            }
        }
        List<RoomPropertyRoomdata> roomPropertyRoomdataList = roomPropertyRoomdataMapper.selectRoomPropertyRoomdataListByRoomId(room1.getRoomId());
        List<String> roomProperty=new ArrayList<>();
        if (roomPropertyRoomdataList!=null) {
            for (RoomPropertyRoomdata roomPropertyRoomdata : roomPropertyRoomdataList) {
                Long propertyRoomDataId = roomPropertyRoomdata.getPropertyRoomDataId();
                if (propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataId(propertyRoomDataId)!=null){
                    PropertyRoomdata propertyRoomdata = propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataId(propertyRoomDataId);
                    roomProperty.add(propertyRoomdata.getPropertyRoomName());
                }
            }
        }
        room1.setRoomProperty(roomProperty);
        return room1;
    }

    /**
     * 查询房间管理列表
     *
     * @param room 房间管理
     * @return 房间管理
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        List<Room> rooms = roomMapper.selectRoomList(room);
        List<Room> roomList = new ArrayList<>();
        for (Room room1 : rooms) {
            room1.getFlatsList().forEach(flats -> {
                room1.setFlatsName(flats.getFlatsName());
                room1.setFlatsId(flats.getFlatsId());
            });
            RoomImage roomImage =roomImageMapper.selectRoomImageByRoomId(room1.getRoomId());
            if(roomImage!=null) {
                if(imageMapper.selectImageByImageId(roomImage.getImageId())!=null) {
                    Image image = imageMapper.selectImageByImageId(roomImage.getImageId());
                    room1.setRoomImagePath(image.getImagePath());
                }
            }
            List<RoomPropertyRoomdata> roomPropertyRoomdataList = roomPropertyRoomdataMapper.selectRoomPropertyRoomdataListByRoomId(room.getRoomId());
            List<String> roomProperty=new ArrayList<>();
            if (roomPropertyRoomdataList!=null) {
                for (RoomPropertyRoomdata roomPropertyRoomdata : roomPropertyRoomdataList) {
                    Long propertyRoomDataId = roomPropertyRoomdata.getPropertyRoomDataId();
                    if (propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataId(propertyRoomDataId)!=null){
                        PropertyRoomdata propertyRoomdata = propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataId(propertyRoomDataId);
                        roomProperty.add(propertyRoomdata.getPropertyRoomName());
                    }
                }
            }
            room1.setRoomProperty(roomProperty);
            roomList.add(room1);
        }
        return roomList;
    }

    /**
     * 新增房间管理
     *
     * @param room 房间管理
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        int i = roomMapper.insertRoom(room);
        FlatsRoom flatsRoom = new FlatsRoom();
        flatsRoom.setRoomId(room.getRoomId());
        flatsRoom.setFlatsId(room.getFlatsId());
        flatsRoomMapper.insertFlatsRoom(flatsRoom);

        if(room.getRoomImagePath()!=null) {
            RoomImage roomImage = new RoomImage();
            roomImage.setRoomId(room.getRoomId());
            if(imageMapper.selectImageByImagePath(room.getRoomImagePath())!=null) {
                roomImage.setImageId(imageMapper.selectImageByImagePath(room.getRoomImagePath()).getImageId());
            }else {
                Image image = new Image();
                image.setImagePath(room.getRoomImagePath());
                image.setImageType(IMAGETYPE);
                image.setCreateTime(new Date());
                imageMapper.insertImage(image);
                roomImage.setImageId(image.getImageId());
            }
            roomImageMapper.insertRoomImage(roomImage);
        }
        if(room.getRoomProperty()!=null){
            List<String> roomProperty = room.getRoomProperty();
            RoomPropertyRoomdata roomPropertyRoomdata = new RoomPropertyRoomdata();
            roomPropertyRoomdata.setRoomId(room.getRoomId());
            for (String roomPropertystr : roomProperty) {
                if(propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataName(roomPropertystr)!=null) {
                    PropertyRoomdata propertyRoomdata = propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataName(roomPropertystr);
                    roomPropertyRoomdata.setPropertyRoomDataId(propertyRoomdata.getPropertyRoomDataId());
                }
                roomPropertyRoomdataMapper.insertRoomPropertyRoomdata(roomPropertyRoomdata);
            }
        }
        return i;
    }

    /**
     * 修改房间管理
     *
     * @param room 房间管理
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        int i = roomMapper.updateRoom(room);
        FlatsRoom flatsRoom = new FlatsRoom();
        flatsRoom.setRoomId(room.getRoomId());
        flatsRoom.setFlatsId(room.getFlatsId());
        flatsRoomMapper.updateFlatsRoomByRoom(flatsRoom);

        roomImageMapper.deleteRoomImageByRoomId(room.getRoomId());
        if(room.getRoomImagePath()!=null) {
            RoomImage roomImage = new RoomImage();
            roomImage.setRoomId(room.getRoomId());
            if(imageMapper.selectImageByImagePath(room.getRoomImagePath())!=null) {
                roomImage.setImageId(imageMapper.selectImageByImagePath(room.getRoomImagePath()).getImageId());
                roomImageMapper.insertRoomImage(roomImage);
            }else {
                roomImageMapper.deleteRoomImageByRoomId(room.getRoomId());
                Image image = new Image();
                image.setImagePath(room.getRoomImagePath());
                image.setImageType(IMAGETYPE);
                image.setCreateTime(new Date());
                imageMapper.insertImage(image);
                roomImage.setImageId(image.getImageId());
                roomImageMapper.insertRoomImage(roomImage);
            }
        }

        roomPropertyRoomdataMapper.deleteRoomPropertyRoomdataByRoomId(room.getRoomId());
        if(room.getRoomProperty()!=null){
            List<String> roomProperty = room.getRoomProperty();
            RoomPropertyRoomdata roomPropertyRoomdata = new RoomPropertyRoomdata();
            roomPropertyRoomdata.setRoomId(room.getRoomId());
            for (String roomPropertystr : roomProperty) {
                if(propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataName(roomPropertystr)!=null) {
                    PropertyRoomdata propertyRoomdata = propertyRoomdataMapper.selectPropertyRoomdataByPropertyRoomDataName(roomPropertystr);
                    roomPropertyRoomdata.setPropertyRoomDataId(propertyRoomdata.getPropertyRoomDataId());
                }
                roomPropertyRoomdataMapper.insertRoomPropertyRoomdata(roomPropertyRoomdata);
            }
        }
        return  i;
    }

    /**
     * 批量删除房间管理
     *
     * @param roomIds 需要删除的房间管理主键
     * @return 结果
     */
    @Override
    public int deleteRoomByRoomIds(Long[] roomIds) {
        roomPropertyRoomdataMapper.deleteRoomPropertyRoomdataByRoomIds(roomIds);
        flatsRoomMapper.deleteFlatsRoomByRoomIds(roomIds);
        roomImageMapper.deleteRoomImageByRoomIds(roomIds);
        return roomMapper.deleteRoomByRoomIds(roomIds);
    }

    /**
     * 删除房间管理信息
     *
     * @param roomId 房间管理主键
     * @return 结果
     */
    @Override
    public int deleteRoomByRoomId(Long roomId) {
        roomPropertyRoomdataMapper.deleteRoomPropertyRoomdataByRoomId(roomId);
        roomImageMapper.deleteRoomImageByRoomId(roomId);
        flatsRoomMapper.deleteFlatsRoomByRoomId(roomId);
        return roomMapper.deleteRoomByRoomId(roomId);
    }

    @Override
    public List<Room> selectAllRoomList(Room room) {
        return roomMapper.selectAllRoomList(room);
    }
}